#include "Sample.h"

using namespace Noise;

static int imageWidth = 0;
static int imageHeight = 0;

void CreateContext (int width, int height, int previewFlags)
{
	nCreateContext( CONTEXT_DEFAULT_FLAGS );

	int size[2] = { width, height };
	nSetContextInteger( IMAGE_VIEWER_SIZES, size, 2 );
	nSetContextInteger( IMAGE_VIEWER_FLAGS, &previewFlags, 1 );

	imageWidth = width;
	imageHeight = height;
}


void Render (const noise::module::Module &outputModule, int channels)
{
	Program prog = nCreateProgram( OPENCL_PROGRAM );
	nBuildProgram( prog, outputModule._GetSource(), EBuildOptions( DEFAULT_OPTIONS /*| BUILD_OUTPUT_UNSIGNED_INT*/ ) );

	int show_preview = false;
	nSetProgramInteger( prog, SHOW_IMAGE_PREVIEW, &show_preview, 1 );

	EPixelFormat pixelFormats[] = { R, RG, RGB, RGBA };

	channels--;
	if ( channels < 0 )		channels = 0;
	if ( channels > 3 )		channels = 3;

	//float * data = new float [ imageWidth * imageHeight * (channels+1) ];
	//nRenderToMemory( prog, imageWidth, imageHeight, 0, 0, 0, pixelFormats[channels], FLOAT32, data );

	nRenderToImage( prog, imageWidth, imageHeight, 0, pixelFormats[channels], UINT8, BMP, "img.bmp" );

	//delete[] data;

	nDeleteProgram( prog );
}


int main ()
{
	RunSample();
	nDeleteContext();
	return 0;
}